package com.xiaoyudeguang.auth;

import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.stp.StpUtil;
import com.xiaoyudeguang.annotations.RequestMapping;
import com.xiaoyudeguang.annotations.RestController;
import com.xiaoyudeguang.auth.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * 登录接口
 *
 * @author 赵光
 */
@ConditionalOnBean(IUserService.class)
@RestController(value = "/login", tags = "登录接口", author = "赵光")
public class LoginController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/doLogin", summary = "登录", method = {RequestMethod.POST, RequestMethod.GET}, checkAuth = false)
    public Map<String, Object> login(@RequestParam(value = "userNo") String userNo, @RequestParam(value = "password") String password) throws SaTokenException {
        Map<String, Object> userInfo = userService.getUser(userNo, password);
        if (userInfo == null) {
            throw new SaTokenException("用户不存在");
        }
        StpUtil.login(userNo);
        StpUtil.getSession().refreshDataMap(userInfo);
        return userInfo;
    }
}
